﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.IO;
using DocManagerORM.Entità;
using DocManagerControls.CLASSI;


namespace DocManagerControls.Controls
{
    /// <summary>
    /// Logica di interazione per FEEDReaderControl.xaml
    /// </summary>
    public partial class FEEDReaderControl : BaseCusControl
    {
        private delegate void UICallerDelegate();

        string m_Errori = "";

        string m_HTML = string.Empty;

        public FEEDReaderControl()
        {
            InitializeComponent();

            RSSclass rss = new RSSclass("Notizie Intel","http://finance.yahoo.com/rss/headline?s=intc" );

            listVCanali.DisplayMemberPath = "Titolo";
            listVCanali.SelectedValuePath = "URL";

            listVCanali.Items.Add( rss);  

        }

        private void listVCanali_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            object obj = listVCanali.SelectedValue ;

            string url = obj.ToString()  ;
			
            this.Cursor = Cursors.Wait;
			Thread thread = new Thread(new ParameterizedThreadStart( readRss) );
			thread.Start( url  );
			this.Cursor = Cursors.AppStarting ;				

        }//fM

        #region RSS

        private void readRss(object pRssURL)
		{
			RssReader rssReader = new RssReader();
			rssReader.RdfMode =false;// this.checkBoxRdf.Checked;
			rssReader.FeedLoaded += new EventHandler(rssReader_FeedLoaded);
			rssReader.ItemAdded  += new EventHandler(rssReader_ItemAdded);
			rssReader.Error      += new RssReaderErrorEventHandler(rssReader_Error);
                                    

			RssFeed feed = rssReader.Retrieve(pRssURL.ToString());
         
            m_HTML = string.Empty;

            if (feed.ErrorMessage == null || feed.ErrorMessage == "")
            {
                m_HTML = RssReader.CreateHtml(feed, getTemplate(), getItemPrefix(), "", 10);
                ShowInBrowser();
            }               
		}//fM

       
	
		private void rssReader_FeedLoaded(object sender, System.EventArgs e)
        {
            SetTextSuccesso();
		}

		private void rssReader_ItemAdded(object sender, System.EventArgs e)
		{
			//this.itemsFound++;
            //txtMessaggi.Text =  " items found.";
		}

		private void rssReader_Error(object sender, RssReaderErrorEventArgs e)
		{
             m_Errori = "Error occured: " + e.Message;
		}
		#endregion
        

        private string getTemplate()
        {
            return  @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">
                <html>
                <head>
                <title>RSS feed</title>
                <meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"">
                <style>
                .channelTitle {
	                font-family: Arial, Helvetica, sans-serif;
	                font-size: 10pt;
	                font-weight: bold;
	                color: #FFFFFF;
                }
                .channelDescription {
	                font-family: Verdana, Arial, Helvetica, sans-serif;
	                font-size: 7pt;
	                color: #003366;
                }
                .itemTitle {
	                font-family: Arial, Helvetica, sans-serif;
	                font-size: 9pt;
	                text-decoration:none;
	                background-color: #F6F6F6;
	                color: #000000;
	                font-weight: bold;
                }
                .itemTitle:hover { color:#003366; }
                .itemDescription {
	                font-family: Verdana, Arial, Helvetica, sans-serif;
	                font-size: 9px;
                }
                </style>
                </head>

                <body>
                <table width=""500"" border=""0"" align=""center"" cellpadding=""1"" cellspacing=""0"" bgcolor=""#003399"">
	                <tr><td>
		                <table width=""500"" border=""0"" cellpadding=""5"" cellspacing=""0"" bgcolor=""#FFFFFF"">
        	                <tr><td bgcolor=""#4F4F4F""><a href=""%Link%"" target=""_blank"" class=""channelTitle"">%Title%</a></td>
        	                </tr>
        	                <tr><td class=""channelDescription"">%Description%</td>
        	                </tr>
        	                </table>
	                </td>
	                </tr>
                </table>
                <br>
                <br>
                <table width=""500"" border=""0"" align=""center"" cellpadding=""1"" cellspacing=""0"" bgcolor=""#A3A3A3"">
                <tr>
	                <td>
		                <table width=""500"" border=""0"" cellpadding=""5"" cellspacing=""0"" bgcolor=""#FFFFFF"">
		                %Items%
		                </table>
	                </td>
                </tr>
                </table>
                </body>
                </html>";
        
        
        }//fM


        private string getItemPrefix()
        {
            return "\t\t<tr>\n\t\t\t<td class=\"itemTitle\"><a href=\"%Link%\" target=\"_blank\" class=\"itemTitle" +
               "\">%Title%</a></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td class=\"itemDescription\">%Description%</" +
               "td>\n\t\t</tr>";
        
        }//fM

        
        private void ShowInBrowser()
        {
            if (this.webBrowser1.Dispatcher.CheckAccess())
            {
                webBrowser1.NavigateToString(m_HTML); 
            }
            else
            {
                this.webBrowser1.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new UICallerDelegate(this.ShowInBrowser));
            }
        }//fM

        private void SetTextSuccesso()
        {
            if (this.txtMessaggi.Dispatcher.CheckAccess())
            {
                this.txtMessaggi.Text = "RSS feed prelevato con successo. ";
            }
            else
            {
                this.txtMessaggi.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new UICallerDelegate(this.SetTextSuccesso));
            }
        }//fM


        private void SetTextFailure()
        {
            if (this.txtMessaggi.Dispatcher.CheckAccess())
            {
                this.txtMessaggi.Text = "Errore prelievo : " + m_Errori;
            }
            else
            {
                this.txtMessaggi.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new UICallerDelegate(this.SetTextFailure));
            }
        }//fM

    }//fC
}
